package com.jiangyao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiangyao.common.ApiResult;
import com.jiangyao.common.exception.ServerException;
import com.jiangyao.model.dto.backend.EditStatusDTO;
import com.jiangyao.model.entity.Resume;
import com.jiangyao.dao.IResumeDao;
import com.jiangyao.model.vo.UserSessionVO;
import com.jiangyao.service.ResumeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.javassist.expr.NewArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 简历 服务实现类
 * </p>
 *
 * @author zhoushuangxiang
 * @since 2020-08-12
 */
@Service
public class ResumeServiceImpl extends BaseServiceImpl<IResumeDao, Resume> implements ResumeService {

    @Autowired
    private IResumeDao iResumeDao;

    @Override
    public Resume addResume(Resume resume) throws ServerException{
        try {
            resume.setCreateTime(new Date());
            UserSessionVO apiLoginSession = getApiLoginSession();
            QueryWrapper<Resume> resumeQueryWrapper = new QueryWrapper<>();
            resume.setUserId(apiLoginSession.getUserId());
            //成员简历
            if(resume.getResumeType().equals(1)){
                resumeQueryWrapper.eq("user_id",apiLoginSession.getUserId()).eq("resume_type",1);
                Resume selectOne = iResumeDao.selectOne(resumeQueryWrapper);
                //如果存在则修改
                if(selectOne != null){
                    iResumeDao.update(resume,resumeQueryWrapper);
                }else {
                    //不存在则添加
                    iResumeDao.insert(resume);
                }
                //快照
            }else {
                iResumeDao.insert(resume);
            }
            return resume;
        } catch (Exception e) {
            throw new ServerException(500,"个人简历创建失败");
        }
    }

    @Override
    public Resume getResume() {
        UserSessionVO apiLoginSession = getApiLoginSession();
        QueryWrapper<Resume> resumeQueryWrapper = new QueryWrapper<>();
        resumeQueryWrapper.eq("user_id",apiLoginSession.getUserId()).eq("resume_type",1);
        Resume resume = iResumeDao.selectOne(resumeQueryWrapper);
        return resume;
    }
}
